// AssessmentDetailServiceImpl.java
package com.projects.modular.realty.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.projects.modular.realty.entity.AssessmentDetail;
import com.projects.modular.realty.mapper.AssessmentDetailMapper;
import com.projects.modular.realty.model.params.AssessmentDetailParam;
import com.projects.modular.realty.service.AssessmentDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 诊断详情服务实现类
 *
 * @author 曾大顺
 * @since 2025-02-11
 */
@Service
public class AssessmentDetailServiceImpl extends ServiceImpl<AssessmentDetailMapper, AssessmentDetail> implements AssessmentDetailService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AssessmentDetailParam assessmentDetailParam) {
        AssessmentDetail assessmentDetail = new AssessmentDetail();
        BeanUtils.copyProperties(assessmentDetailParam, assessmentDetail);
        assessmentDetail.setContent(JSONObject.toJSONString(assessmentDetailParam.getContent()));
        assessmentDetail.setCreateTime(LocalDateTime.now());
        this.save(assessmentDetail);
    }

    @Override
    public void deleteById(String assessmentDetailId) {
        this.removeById(assessmentDetailId);
    }

    @Override
    public void deleteByDiagnosisId(String diagnosisId){
        QueryWrapper<AssessmentDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssessmentDetail::getDiagnosisId,diagnosisId);
        this.remove(wrapper);
    }

    @Override
    public void update(AssessmentDetailParam assessmentDetailParam) {
        AssessmentDetail assessmentDetail = new AssessmentDetail();
        BeanUtils.copyProperties(assessmentDetailParam, assessmentDetail);
        this.updateById(assessmentDetail);
    }

    @Override
    public AssessmentDetail getById(String assessmentDetailId) {
        return this.getById(assessmentDetailId);
    }

    @Override
    public List<AssessmentDetail> getByDiagnosisId(String diagnosisId) {
        return this.baseMapper.selectByDiagnosisId(diagnosisId);
    }
}
